// d:\byte\tiktok-applet-v3 - 智慧天气 - 家具\pages\conmmentadd\conmmentadd.js
const model = require('../../public/model/env.js');

const url = model.api_pro_url(); // 线上环境
const key = model.key();
// const appid = model.appid();

const userid = tt.getStorageSync('userid');


Page({
  data: {
    title: '评论',
    pay_order_no: '',
    value: '',
    comment_date: '',
  },
  onLoad: function (options) {
    const opt = options.pay_order_no
    this.cx(opt);


    console.log(options.pay_order_no);

    this.setData({
      pay_order_no: options.pay_order_no,
    });
    // 页面加载,标题设置
    tt.setNavigationBarTitle({
      title: this.data.title,
    });
    tt.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000',
    });
    // 页面加载,标题设置结束


  },
  cx(opt) {
    const opt = opt;
    const numericUserId = parseInt(userid, 10);
    const requestUrl = `${url}/index.php/api/comment_cx.php`;
    const options = {
      url: requestUrl,
      data: {
        key: key,
        userid: numericUserId,
        pay_order_no: opt,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    console.log(options);
    tt.request({
      ...options,
      success: (res) => {
        const data = res.data;
        console.log(data.data[0]);
        if (data.code == 200) {
          let text = '';
          if (data.data[0].comment_status === 1) {
            text = '未发表';
          } else if (data.data[0].comment_status === 2) {
            text = '已发表';
          }
          this.setData({
            value: data.data[0].comment,
            comment_date: data.data[0].comment_date,
            comment_status: text,
          });


        }
        else {
          tt.showToast({
            title: data.message,
            icon: 'none',
            duration: 1500,
            mask: false,
          });
        }
      },
      fail: (err) => {
      }
    })

  },
  tijiao_pinglun() {
    if (this.data.value.length < 10) {
      tt.showToast({
        title: '评论字数不能太少',
        icon: 'none',
        duration: 1500,
        mask: false,
      });
      return;
    }

    const numericUserId = parseInt(userid, 10);
    const requestUrl = `${url}/index.php/api/comment_add.php`;
    const options = {
      url: requestUrl,
      data: {
        // appid: appid,
        key: key,
        userid: numericUserId,
        comment_text: this.data.value,
        pay_order_no: this.data.pay_order_no
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    // console.log(options);
    tt.request({
      ...options,
      success: (res) => {
        const data = res.data;
        console.log(data);
        if (data.code == 200) {
          tt.showToast({
            title: '评论成功',
            icon: 'none',
            duration: 1500,
            mask: false,
          });


        }
        else {
          tt.showToast({
            title: data.message,
            icon: 'none',
            duration: 1500,
            mask: false,
          });
        }
      },
      fail: (err) => {
      }
    })
  },
  handleInput(e) {
    console.log("input", e.detail);
    this.setData({
      value: e.detail.value
    })
  },

  handleSwitchBlur() {
    this.setData({
      isFocus: false,
    }, () => {
      tt.showToast({
        title: 'blur',
        duration: 1500,
        icon: 'none',
        mask: false,
      });
    });
  }
})